import 'package:check_app/dao/userInfo_dao.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class MyPasswordPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _MyPasswordPageState();
}

class _MyPasswordPageState extends State<MyPasswordPage> {
  bool _loading = false;
  TextEditingController _oldPassword = new TextEditingController();
  TextEditingController _newPassword = new TextEditingController();
  TextEditingController _confirmPassword = new TextEditingController();
  GlobalKey _formKey = new GlobalKey<FormState>();

  @override
  void initState() {
    super.initState();
  }

  Future<Null> submit() async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await UserInfoDao.savePassword({
        'oldPassword': _oldPassword.text,
        'password': _newPassword.text,
        'rePassword': _confirmPassword.text,
      });
      setState(() {
        _loading = false;
      });
      if (result.success) {
        Navigator.of(context).pop();
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('修改密码'),
        actions: <Widget>[
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              if ((_formKey.currentState as FormState).validate()) {
                submit();
              }
            },
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
              child: Text('提交'),
            ),
          ),
        ],
      ),
      body: Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(top: 16),
            decoration: BoxDecoration(
              color: Color.fromRGBO(255, 255, 255, 1),
              border: Border(
                top: BorderSide(
                  width: 0.5,
                  color: Color.fromRGBO(229, 229, 229, 1),
                ),
              ),
            ),
            child: Form(
              key: _formKey,
              autovalidate: false,
              child: Column(
                children: <Widget>[
                  // 原始密码
                  Container(
                    height: 62,
                    width: double.infinity,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          width: 0.5,
                          color: Color.fromRGBO(229, 229, 229, 1),
                        ),
                      ),
                    ),
                    child: Row(
                      children: <Widget>[
                        Container(
                          width: 120,
                          padding: EdgeInsets.only(left: 16),
                          child: Text(
                            '原始密码',
                            style: TextStyle(
                              fontSize: 16,
                              height: 19 / 16,
                            ),
                          ),
                        ),
                        Expanded(
                          child: TextFormField(
                            autofocus: false,
                            obscureText: true,
                            controller: _oldPassword,
                            style: TextStyle(
                              fontSize: 15.5,
                              height: 22.5 / 15.5,
                              color: Color.fromRGBO(43, 44, 46, 1),
                            ),
                            decoration: InputDecoration(
                              fillColor: Colors.pink,
                              contentPadding: EdgeInsets.fromLTRB(0, 0, 16, 0),
                              hintText: "请输入原始密码",
                              hintStyle: TextStyle(
                                fontSize: 15.5,
                                height: 22.5 / 15.5,
                                color: Color.fromRGBO(181, 184, 189, 1),
                              ),
                              border: OutlineInputBorder(
                                borderSide: BorderSide.none,
                              ),
                            ),
                            validator: (v) {
                              return v.trim().length > 0 ? null : ('原始密码不能为空');
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  // 新密码
                  Container(
                    height: 62,
                    width: double.infinity,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          width: 0.5,
                          color: Color.fromRGBO(229, 229, 229, 1),
                        ),
                      ),
                    ),
                    child: Row(
                      children: <Widget>[
                        Container(
                          width: 120,
                          padding: EdgeInsets.only(left: 16),
                          child: Text(
                            '新密码',
                            style: TextStyle(
                              fontSize: 16,
                              height: 19 / 16,
                            ),
                          ),
                        ),
                        Expanded(
                          child: TextFormField(
                            autofocus: false,
                            obscureText: true,
                            controller: _newPassword,
                            style: TextStyle(
                              fontSize: 15.5,
                              height: 22.5 / 15.5,
                              color: Color.fromRGBO(43, 44, 46, 1),
                            ),
                            decoration: InputDecoration(
                              fillColor: Colors.pink,
                              contentPadding: EdgeInsets.fromLTRB(0, 0, 16, 0),
                              hintText: "请输入新密码",
                              hintStyle: TextStyle(
                                fontSize: 15.5,
                                height: 22.5 / 15.5,
                                color: Color.fromRGBO(181, 184, 189, 1),
                              ),
                              border: OutlineInputBorder(
                                borderSide: BorderSide.none,
                              ),
                            ),
                            validator: (v) {
                              return v.trim().length > 0 ? null : ('新密码不能为空');
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                  // 确认密码
                  Container(
                    height: 62,
                    width: double.infinity,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          width: 0.5,
                          color: Color.fromRGBO(229, 229, 229, 1),
                        ),
                      ),
                    ),
                    child: Row(
                      children: <Widget>[
                        Container(
                          width: 120,
                          padding: EdgeInsets.only(left: 16),
                          child: Text(
                            '确认密码',
                            style: TextStyle(
                              fontSize: 16,
                              height: 19 / 16,
                            ),
                          ),
                        ),
                        Expanded(
                          child: TextFormField(
                            autofocus: false,
                            obscureText: true,
                            controller: _confirmPassword,
                            style: TextStyle(
                              fontSize: 15.5,
                              height: 22.5 / 15.5,
                              color: Color.fromRGBO(43, 44, 46, 1),
                            ),
                            decoration: InputDecoration(
                              fillColor: Colors.pink,
                              contentPadding: EdgeInsets.fromLTRB(0, 0, 16, 0),
                              hintText: "请输入确认密码",
                              hintStyle: TextStyle(
                                fontSize: 15.5,
                                height: 22.5 / 15.5,
                                color: Color.fromRGBO(181, 184, 189, 1),
                              ),
                              border: OutlineInputBorder(
                                borderSide: BorderSide.none,
                              ),
                            ),
                            validator: (v) {
                              return v.trim().length > 0 ? null : ('确认密码不能为空');
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
